/*******************************************************************************
TablesIV_VII_VIII.do

This file creates tables to show span of control results using data created
in "Code\Setup\NETS_Span_of_Control_Data.do". It regresses log firm-location 
average employment on log total firm employment excluding the own firm 
contribution of employment in the location, log firm-location number of plants 
and its square, and industry-location fixed effects. Unless specified, results 
are for M=12.

Table IV: baseline M=12, baseline M=48, firms with at least 100 plants, 
		  industries in which the largest firm has at least 100 plants
		 
Table VII:
1) baseline M=3, 6, 12, 24, 48
2) firms with at least X=10, 20, 50, 100 plants (M=12)
3) industries where the largest firm has at least X=10, 20, 50, 100 plants (M=12)
4) manufacturing, services, retail trade, FIRE (M=12)

Table VIII:
1) using log total firm employment without excluding own firm contribution
2) including higher order terms of log firm-location number of plants
3) using non-imputed data

Last updated: 4/29/2021
*******************************************************************************/

version 15
cd "C:\Plants_in_Space"
set more off
set type double
set varabbrev off
graph set window fontface "Times New Roman" 

***************************Table IV: Span of control****************************
// Baseline M=12
use "Data\Final\TablesIV_VII_VIII\span_of_control_M12_data.dta", clear
gen log_firm_location_num_estab_2 = log(firm_location_num_estab)^2
label variable log_firm_location_num_estab "$\ln n_{js}$"
label variable log_firm_location_num_estab_2 "$(\ln n_{js})^2$"
label variable log_firm_emp_net_location "$\ln L_{j,-js}$"
reghdfe log_firm_location_avg_emp log_firm_emp_net_location log_firm_location_num_estab log_firm_location_num_estab_2, a(industry_loc_group) vce(robust)
outreg2 using "Tables\TableIV.tex", label replace tex(fragment) nocons ///
	ctitle("\textit{Baseline}", "\textit{$ M = 12$}", "$\hspace{3em}\ln \bar{l}_{js}\hspace{3em}$") addtext(SIC8-location FE, Yes, M, 12)

preserve
	// Baseline M=48
	use "Data\Final\TablesIV_VII_VIII\span_of_control_M48_data.dta", clear
	gen log_firm_location_num_estab_2 = log(firm_location_num_estab)^2
	label variable log_firm_location_num_estab "$\ln n_{js}$"
	label variable log_firm_location_num_estab_2 "$(\ln n_{js})^2$"
	label variable log_firm_emp_net_location "$\ln L_{j,-js}$"
	reghdfe log_firm_location_avg_emp log_firm_emp_net_location log_firm_location_num_estab log_firm_location_num_estab_2, a(industry_loc_group) vce(robust)
	outreg2 using "Tables\TableIV.tex", label append tex(fragment) nocons ///
		ctitle("\textit{Baseline}", "\textit{$ M = 48$}", "$\hspace{3em}\ln \bar{l}_{js}\hspace{3em}$") addtext(SIC8-location FE, Yes, M, 48)
restore

preserve
	// Firms with at least X=100 plants
	merge m:1 hq sic8 using "Data\Intermediate\NETS\NETS_HQs_at_least_5_employees_2014_hq-sic8_of_firms_with_at_least_100_plants.dta", keep(3) nogen
	reghdfe log_firm_location_avg_emp log_firm_emp_net_location log_firm_location_num_estab log_firm_location_num_estab_2, a(industry_loc_group) vce(robust)
	outreg2 using "Tables\TableIV.tex", label append tex(fragment) nocons ///
		ctitle("\textit{Firms with}", "\textit{$\geq 100$ plants}", "$\hspace{3em}\ln \bar{l}_{js}\hspace{3em}$") addtext(SIC8-location FE, Yes, M, 12)
restore

preserve
	// Industries where largest firm has at least X=100 plants
	merge m:1 hq sic8 using "Data\Intermediate\NETS\NETS_HQs_at_least_5_employees_2014_hq-sic8_of_firms_in_industries_where_largest_firm_has_at_least_100_plants.dta", keep(3) nogen
	reghdfe log_firm_location_avg_emp log_firm_emp_net_location log_firm_location_num_estab log_firm_location_num_estab_2, a(industry_loc_group) vce(robust)
	outreg2 using "Tables\TableIV.tex", label append tex(fragment) nocons ///
		ctitle("\textit{Industries in which}", "\textit{largest firms has}", "\textit{$\geq 100$ plants}", "$\hspace{3em}\ln \bar{l}_{js}\hspace{3em}$") addtext(SIC8-location FE, Yes, M, 12)
restore

***************************Table VII: Span of Control***************************
// Panel 1: Changing M
foreach m of numlist 3 6 12 24 48 {
	if `m' == 3 {
		local append_replace = "replace"
	}
	else {
		local append_replace = "append"
	}
	use "Data\Final\TablesIV_VII_VIII\span_of_control_M`m'_data.dta", clear
	gen log_firm_location_num_estab_2 = log(firm_location_num_estab)^2
	label variable log_firm_location_num_estab "$\ln n_{js}$"
	label variable log_firm_location_num_estab_2 "$(\ln n_{js})^2$"
	label variable log_firm_emp_net_location "$\ln L_{j,-js}$"
	reghdfe log_firm_location_avg_emp log_firm_emp_net_location log_firm_location_num_estab log_firm_location_num_estab_2, a(industry_loc_group) vce(robust)
	outreg2 using "Tables\TableVII_panel1.tex", label `append_replace' tex(fragment) nocons nonotes ///
		ctitle("$\ln \bar{l}_{js}$") addtext(SIC8-location FE, Yes, M, `m')

	if `m' == 12 {
		outreg2 using "Tables\TableVII_panel2.tex", label replace tex(fragment) nocons nonotes ///
			ctitle("\textit{Baseline}") addtext(SIC8-location FE, Yes, M, `m')
		outreg2 using "Tables\TableVII_panel3.tex", label replace tex(fragment) nocons nonotes ///
			ctitle("\textit{Baseline}") addtext(SIC8-location FE, Yes, M, `m')
		outreg2 using "Tables\TableVII_panel4.tex", label replace tex(fragment) nocons nonotes ///
			ctitle("\textit{Baseline}") addtext(SIC8-location FE, Yes, M, `m')
	}
}

// Panel 2: firms with at least X plants
use "Data\Final\TablesIV_VII_VIII\span_of_control_M12_data.dta", clear
gen log_firm_location_num_estab_2 = log(firm_location_num_estab)^2
label variable log_firm_location_num_estab "$\ln n_{js}$"
label variable log_firm_location_num_estab_2 "$(\ln n_{js})^2$"
label variable log_firm_emp_net_location "$\ln L_{j,-js}$"
foreach X of numlist 10 20 50 100 {
	preserve
		merge m:1 hq sic8 using "Data\Intermediate\NETS\NETS_HQs_at_least_5_employees_2014_hq-sic8_of_firms_with_at_least_`X'_plants.dta", keep(3) nogen
		reghdfe log_firm_location_avg_emp log_firm_emp_net_location log_firm_location_num_estab log_firm_location_num_estab_2, a(industry_loc_group) vce(robust)
		outreg2 using "Tables\TableVII_panel2.tex", label append tex(fragment) nocons nonotes ///
			ctitle("\textit{X = `X'}") addtext(SIC8-location FE, Yes, M, 12)
	restore
}

// Panel 3: Industries where largest firm has at least X plants
use "Data\Final\TablesIV_VII_VIII\span_of_control_M12_data.dta", clear
gen log_firm_location_num_estab_2 = log(firm_location_num_estab)^2
label variable log_firm_location_num_estab "$\ln n_{js}$"
label variable log_firm_location_num_estab_2 "$(\ln n_{js})^2$"
label variable log_firm_emp_net_location "$\ln L_{j,-js}$"
foreach X of numlist 10 20 50 100 {
	preserve
		merge m:1 hq sic8 using "Data\Intermediate\NETS\NETS_HQs_at_least_5_employees_2014_hq-sic8_of_firms_in_industries_where_largest_firm_has_at_least_`X'_plants.dta", keep(3) nogen
		reghdfe log_firm_location_avg_emp log_firm_emp_net_location log_firm_location_num_estab log_firm_location_num_estab_2, a(industry_loc_group) vce(robust)
		outreg2 using "Tables\TableVII_panel3.tex", label append tex(fragment) nocons nonotes ///
			ctitle("\textit{X = `X'}") addtext(SIC8-location FE, Yes, M, 12)
	restore
}

// Panel 4: By major industry
use "Data\Final\TablesIV_VII_VIII\span_of_control_M12_data.dta", clear
gen log_firm_location_num_estab_2 = log(firm_location_num_estab)^2
label variable log_firm_location_num_estab "$\ln n_{js}$"
label variable log_firm_location_num_estab_2 "$(\ln n_{js})^2$"
label variable log_firm_emp_net_location "$\ln L_{j,-js}$"
foreach div in manufacturing services retail FIRE {
	preserve
		if "`div'" == "retail" {
			keep if sic2>=52 & sic2<=59
			local divlabel = "\textit{Retail Trade}"
		}
		else if "`div'" == "services" {
			keep if sic2>=70 & sic2<=89
			local divlabel = "\textit{Services}"
		}
		else if "`div'" == "manufacturing" {
			keep if sic2>=20 & sic2<=39	
			local divlabel = "\textit{Manufacturing}"
		}
		else if "`div'" == "FIRE" {
			keep if sic2>=60 & sic2<=67
			local divlabel = "\textit{FIRE}"
		}
		reghdfe log_firm_location_avg_emp log_firm_emp_net_location log_firm_location_num_estab log_firm_location_num_estab_2, a(industry_loc_group) vce(robust)
		outreg2 using "Tables\TableVII_panel4.tex", label append tex(fragment) nocons nonotes ///
			ctitle("`divlabel'") addtext(SIC8-location FE, Yes, M, 12)
	restore
}

****************Table VIII: Span of Control Additional Exercises****************
// Panel 1: Without excluding own contribution
foreach m of numlist 3 6 12 24 48 {
	if `m' == 3 {
		local append_replace = "replace"
	}
	else {
		local append_replace = "append"
	}
	use "Data\Final\TablesIV_VII_VIII\span_of_control_M`m'_data.dta", clear
	gen log_firm_location_num_estab_2 = log(firm_location_num_estab)^2
	label variable log_firm_location_num_estab "$\ln n_{js}$"
	label variable log_firm_location_num_estab_2 "$(\ln n_{js})^2$"
	label variable log_firm_employment "$\ln L_{j}$"
	reghdfe log_firm_location_avg_emp log_firm_employment log_firm_location_num_estab log_firm_location_num_estab_2, a(industry_loc_group) vce(robust)
	outreg2 using "Tables\TableVIII_panel1.tex", label `append_replace' tex(fragment) nocons nonotes ///
		ctitle("$\ln \bar{l}_{js}$") addtext(SIC8-location FE, Yes, M, `m')
}

// Panel 2: Including higher order terms of log firm-location number of plants
foreach m of numlist 3 6 12 24 48 {
	if `m' == 3 {
		local append_replace = "replace"
	}
	else {
		local append_replace = "append"
	}
	use "Data\Final\TablesIV_VII_VIII\span_of_control_M`m'_data.dta", clear
	forvalues i = 2/4 {
		gen log_firm_location_num_estab_`i' = log(firm_location_num_estab)^`i'
	}
	label variable log_firm_location_num_estab "$\ln n_{js}$"
	label variable log_firm_location_num_estab_2 "$(\ln n_{js})^2$"
	label variable log_firm_location_num_estab_3 "$(\ln n_{js})^3$"
	label variable log_firm_location_num_estab_4 "$(\ln n_{js})^4$"
	label variable log_firm_emp_net_location "$\ln L_{j,-js}$"
	reghdfe log_firm_location_avg_emp log_firm_emp_net_location log_firm_location_num_estab log_firm_location_num_estab_2 log_firm_location_num_estab_3 log_firm_location_num_estab_4, a(industry_loc_group) vce(robust)
	outreg2 using "Tables\TableVIII_panel2.tex", label `append_replace' tex(fragment) nocons nonotes ///
		ctitle("  ") addtext(SIC8-location FE, Yes, M, `m')
}

// Panel 3: Using only non-imputed data
foreach m of numlist 3 6 12 24 48 {
	if `m' == 3 {
		local append_replace = "replace"
	}
	else {
		local append_replace = "append"
	}
	use "Data\Final\TablesIV_VII_VIII\span_of_control_M`m'_nonimputed_data.dta", clear
	gen log_firm_location_num_estab_2 = log(firm_location_num_estab)^2
	label variable log_firm_location_num_estab "$\ln n_{js}$"
	label variable log_firm_location_num_estab_2 "$(\ln n_{js})^2$"
	label variable log_firm_emp_net_location "$\ln L_{j,-js}$"
	reghdfe log_firm_location_avg_emp log_firm_emp_net_location log_firm_location_num_estab log_firm_location_num_estab_2, a(industry_loc_group) vce(robust)
	outreg2 using "Tables\TableVIII_panel3.tex", label `append_replace' tex(fragment) nocons nonotes ///
		ctitle("  ") addtext(SIC8-location FE, Yes, M, `m')
}
